package aspirecn.project3.manager.Impl;


import aspirecn.project3.manager.UserManager;
import aspirecn.project3.mapper.UserMapper;
import aspirecn.project3.model.User;
import aspirecn.project3.util.CheckUtil;
import aspirecn.project3.util.JsonData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

@Service
@Slf4j
public class UserManagerImpl implements UserManager {

    @Autowired
    UserMapper userMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public JsonData addUser(User user) {

        log.info("{}", userMapper.getUserByPhone(user.getPhone()));
        if (userMapper.getUserByPhone(user.getPhone()) != null) {
            return JsonData.buildError("该手机号已被注册");
        } else if (userMapper.getUserByEmail(user.getEmail()) != null) {
            return JsonData.buildError("该邮箱已经被注册");
        } else {
            user.setPassword(passwordEncoder.encode(user.getPassword()));
            //  user.setPassword(user.getPassword());
            userMapper.addUser(user);
            return JsonData.buildSuccess();
        }
    }

    @Override
    public User getUserByUsername(String username) {
        if (CheckUtil.isPhone(username)) {
            User userById = userMapper.getUserById(userMapper.getUserByPhone(username));
            log.info("{}", userById);
            if (userById != null) {
                return userById;
            } else {
                return null;
            }
        } else if (CheckUtil.isEmail(username)) {
            User userById = userMapper.getUserById(userMapper.getUserByEmail(username));
            if (userById != null) {
                return userById;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    @Override
    public List<User> getUserByPage(Integer page, Integer size) {
        return userMapper.getUserByPage(page, size);
    }

    @Override
    public Long getTotal() {
        return userMapper.getTotal();
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public User getUserById(Integer id) {
        return userMapper.getUserById(Long.valueOf(id));
    }

    @Override
    public Integer insertUser(User user) {
        return userMapper.insertUser(user);
    }

    @Override
    public List<User> getUserByName(Integer page, Integer size, String keyword) {
        return userMapper.getUserByName(page, size, keyword);
    }

    @Override
    public Integer alterUser(User user) {
        if(userMapper.alterUser(user)==1){
            return 1;
        }
        return 0;
    }

    @Override
    public User getRandomUser() {
        int total = userMapper.getTotal().intValue();
        Random random = new Random();
        return userMapper.getRandomUser(random.nextInt(total));
    }
}
